import redis

pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=1, decode_responses=True)
redis_client = redis.Redis(connection_pool=pool)

# redis_client.set('name', '大周老师')
# print(redis_client.get('name'))
#
# redis_client.mset({'age': 18, 'address': 'beijing'})
# print(redis_client.mget('age', 'address'))
# print(redis_client.exists('name'))
#
# print(redis_client.dbsize())
# print(redis_client.lastsave())


# ================  2. hash类型   ====================
# redis_client.hset(name='userHash', key='username', value='dazhoulaoshi')
# redis_client.hset(name='userHash2', mapping={
#     'username': '大周老师',
#     'password': '123456',
#     'nickname': '大大',
#     'address': '北京'
# })
# print(redis_client.hget(name='userHash2', key='username'))
# print(redis_client.hgetall(name='userHash2'))


# ================  3. list类型   ====================
# redis_client.lpush('numberLeft', 1, 2, 3)
# redis_client.rpush('numberRight', 1, 2, 3, 4, 5)
# for i in range(redis_client.llen('numberRight')):
#     print(redis_client.lindex('numberRight', i))


# ================  4. set类型   ====================
# redis_client.sadd('setNum', 11, 23, 5, 23)
# set_number = redis_client.smembers('setNum')
# print(set_number)


# ================  5. zset类型   ====================
redis_client.zadd('myzset', {'v1': 10, 'v2': 20, 'v3': 30})
r = redis_client.zrangebyscore('myzset', 10, 20)
print(r)

r = redis_client.zrangebyscore('myzset', 10, 20, withscores=True)
print(r)
print(redis_client.zrank('myzset', 'v3'))
